﻿using Dapper;
using Entity;
using IRealize;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Realize
{
    public class SSD : ISSD
    {
        public async Task<int> Delete(int id, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"delete from salary_standard_details where sdt_id = ${id}";
                return await con.ExecuteAsync(sql);

            }
        }

        public async Task<int> Insert(string standard_id,string standard_name,int item_id, string item_name,double salary, string sqlserver)
        {

            using (SqlConnection sqlConnection = new SqlConnection(sqlserver))
            {
                string sql = $@"insert into salary_standard_details(standard_id,standard_name,item_id,item_name,salary)
                 values('{standard_id}','{standard_name}',{item_id},'{item_name}',{salary}) ";
                return await sqlConnection.ExecuteAsync(sql);
            }
        }

        public async Task<List<Salary_Standard_Details>> Server(string sqlserver)
        {
            Task<List<Salary_Standard_Details>> task = Task<List<Salary_Standard_Details>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_standard_details";
                    return con.Query<Salary_Standard_Details>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Salary_Standard_Details>> Server2(int id ,string sqlserver)
        {
            Task<List<Salary_Standard_Details>> task = Task<List<Salary_Standard_Details>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_standard_details where sdt_id = {id}";
                    return con.Query<Salary_Standard_Details>(sql).ToList();
                }
            });
            return await task;
        }
        public async Task<List<Salary_Standard_Details>> Server3(string sid ,string sqlserver)
        {
            Task<List<Salary_Standard_Details>> task = Task<List<Salary_Standard_Details>>.Run(() =>
            {
                using (SqlConnection con = new SqlConnection(sqlserver))
                {

                    string sql = $"select * from salary_standard_details where standard_id = '{sid}'";
                    return con.Query<Salary_Standard_Details>(sql).ToList();
                }
            });
            return await task;
        }

        public async Task<int> Update(int id, string item_name, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update salary_standard_details set item_name = '{item_name}' WHERE sdt_id = {id}";
                return await con.ExecuteAsync(sql);

            }
        }

        public async Task<int> Update2(int sdt_id, double salary, string sqlserver)
        {
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = $"update salary_standard_details set salary = {salary} WHERE sdt_id = {sdt_id}";
                return await con.ExecuteAsync(sql);

            }
        }
    }
}
